<template>
	<section>
		<!--工具条-->
    <!--高级查询-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" >
				<el-form-item>
					<el-input v-model="keyWord" placeholder="请输入关键字！！！"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="typeQuery">查询</el-button>
				</el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
			</el-form>
		</el-col>

    <el-radio-group v-model="direction">
    </el-radio-group>




<!-- de表 -->
    <el-drawer
        title="title"
        :visible.sync="drawer"
        :direction="direction"
        :before-close="handleClose">
      <template>
        <section>
          <!--工具条-->
          <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true">
              <el-form-item>
                <el-input v-model="keyword" placeholder="请输入关键字"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" v-on:click="keywordQuery">高级查询</el-button>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="handleAdd1">新增</el-button>
              </el-form-item>
            </el-form>
          </el-col>

          <!--列表-->
          <el-table :data="dictionarydetails" highlight-current-row
                    v-loading="listLoading1" @selection-change="selsChange1" >
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="字典明细名称" width="80" sortable>
            </el-table-column>
            <el-table-column prop="dictionaryType.name" label="字典明细类型" width="70" sortable>
            </el-table-column>
            <el-table-column label="操作">
              <template scope="scope">
                <el-button size="small" @click="handleEdit1(scope.$index, scope.row)">编辑</el-button>

                <el-button type="danger" size="small" @click="handleDel1(scope.$index, scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <!--工具条-->
          <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove1" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next"
                           @current-change="handleCurrentChange1" :page-size="pageSize1" :current-page="currentPage1"
                           :total="totals1" style="float:right;">
            </el-pagination>
          </el-col>

          <!--编辑界面-->
          <el-dialog :append-to-body="true" title="编辑" :visible.sync="saveFormVisible1" :close-on-click-modal="false">
            <el-form :model="saveForm1" label-width="120px" :rules="saveFormRules1" ref="saveForm">
              <el-form-item label="字典明细名称" prop="name">
                <el-input v-model="saveForm1.name" auto-complete="off"></el-input>
              </el-form-item>

              <el-form-item label="字典明细类型" prop="types_id">
                <el-select v-model="saveForm1.types_id" placeholder="请选择">
                  <el-option
                      v-for="item in types"
                      :label="item.name"
                      :value="item.id">
                    <span style="float: left">{{ item.name }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click.native="saveFormVisible1 = false">取消</el-button>
              <el-button type="primary" @click.native="saveSubmit1" :loading="saveLoading1">提交</el-button>
            </div>
          </el-dialog>

        </section>
      </template>

    </el-drawer>

    <!--  type表   -->

    <!--列表-->
		<el-table @cell-dblclick="ss"  :data="dictionaryTypes" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
      <el-table-column   prop="sn" label="字典编号" width="120" sortable>
      </el-table-column>

      <el-table-column  prop="name" label="字典名称" width="120" sortable>
      </el-table-column>

      <el-table-column label="操作" width="230">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button @click="drawer = true"  size="small" type="primary" style="margin-left: 16px;">
            点击</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :current-page="currentPage" :page-size="pageSize"
                     :total="totals" style="float:right;">
			</el-pagination>
		</el-col>
		<!--编辑界面-->
		<el-dialog :title="title"  :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="字典编号" prop="sn">
					<el-input v-model="saveForm.sn" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>




<script>

	export default {
		data() {
			return {

			  // type 表数据
			  // 分页数据
        dictionaryTypes: [],  //绑定到表格 - 显示列表数据
        totals: 0,     //总数量 - 计算分页栏
			  currentPage:1,   //当前页 - 分页栏默认当前页显示
        pageSize:5,      //每页显示的条数  - 计算分页栏
        listLoading: false,  //表格上的加载框

        drawer: false,
        direction: 'rtl',

        //高级查询
          keyWord:'',

        // 批量删除
				sels: [],//列表选中列

				saveFormVisible: false,//编辑界面是否显示
				saveLoading: false,
        title: '',
        saveFormRules: {
					sn: [
						{ required: true, message: '请输入字典编号', trigger: 'blur' }
					],name: [
          { required: true, message: '请输入字典名称', trigger: 'blur' }
        ]
				},
				//编辑界面数据
				saveForm: {
					id: null,
          sn: '',
					name: ''
				},




        // detail表
        // 分页查询的参数
        dictionarydetails: [],   // 先赋值为空，绑定到表格
        totals1: 0,       // 数据的总数量
        currentPage1: 1,  // 当前页
        pageSize1: 5,     // 当前页显示的条数
        listLoading1: false, // 是否显示加载框

        // 高级查询的参数
        keyword: "",

        // 批量删除的数据：因为是多条数据所以用数组
        sels1: [],//列表选中列

        saveFormVisible1: false,//编辑界面是否显示
        saveLoading1: false,   // 是否显示加载框
        title1: "",            // 标题
        saveFormRules1: {      // 检验规则
          name: [
            { required: true, message: '请输入字典明细名称', trigger: 'blur' }
          ],
          types_id: [
            { required: true, message: '请输入字典明细类型', trigger: 'blur' }
          ],

        },
        //编辑界面数据
        saveForm1: {
          id: null,
          name: '',
          types_id: null,
          types: null
        },

        // 字典类型
        types: []



			}
		},
		methods: {
		  ss(){
		    this.drawer= true;
      },

      // type 表
      handleClose(done) {
        this.$confirm('确认关闭？')
            .then(_ => {
              done();
            })
            .catch(_ => {});
      },

      //获取用户列表
      getDictionaryTypes() {
        let para = {
          // 分页数据
          currentPage:this.currentPage,
          pageSize:this.pageSize,

          // 高级查询数据
          keyWord:this.keyWord

        };

        //页面一加载就显示加载框
        this.listLoading = true;
        //NProgress.start();
        this.$http.post("/type",para).then((res) => {
          //赋值总数量
          this.totals = res.data.totals;
          //当前页的数据
          this.dictionaryTypes = res.data.data;
          //关闭加载框
          this.listLoading = false;
          //NProgress.done();
        }).catch(res=>{
          this.$message.error('系统繁忙，请稍后再试');
        });
      },handleCurrentChange(val) {
        this.currentPage = val;
        this.getDictionaryTypes();
      },

      // 高级查询
      typeQuery(){
        this.currentPage=1,
        this.getDictionaryTypes()
      },

      //删除
      handleDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //NProgress.start();
        let  id = row.id ;
         this.$http.delete("/type/"+ id) .then((res) => {

            if (res.data.success){
              this.$message.success('删除成功');
              var totalPage=Math.ceil(this.totals/this.pageSize);
              if(totalPage==this.currentPage &&this.currentPage>1&&(this.totals-1)%this.pageSize==0){
                this.currentPage=this.currentPage-1;
              }


              this.getDictionaryTypes();
            }else{
              this.$message.error('删除失败，请稍后在试[500]！！！');
            }
           this.listLoading = false;
          }).catch(res=>{
           this.$message.error('删除失败，请稍后在试[400,404]！！！');
         });
        }).catch(() => {
        });
      },

      // 批量删除
      selsChange: function (sels) {
        this.sels = sels;
      },
      //批量删除
      batchRemove: function () {
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          this.$http.patch("/type",ids).then(res=>{
            if (res.data.success){
              this.$message.success('删除成功');
              this.getDictionaryTypes();
            }else{
              this.$message.error('删除失败，请稍后在试[500]！！！');
            }
            this.listLoading = false;
          }).catch(res=>{
            this.$message.error('删除失败，请稍后在试[404,400]！！！');
          });
        }).catch(() => {
        });
      },
      //显示新增界面
      handleAdd: function () {
        this.saveForm = {
          id: null,
          sn: '',
          name: ''
        };
        this.title="添加";
        //显示对话框
        this.saveFormVisible = true;
      },

      //显示编辑界面
      handleEdit: function (index, row) {

        this.saveForm = Object.assign({}, row);
        this.title="修改";
        this.saveFormVisible = true;
      },
      //编辑
      saveSubmit: function () {
        //所有对话框中  一个名称为 saveForm的对话框  验证【上面的验证规则通过了 - 返回true，否则返回false】
        this.$refs.saveForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.saveLoading = true;
              let para = Object.assign({}, this.saveForm);

              this.$http.put("/type",para).then(res => {
                if(res.data.success){
                  this.$message.success("操作成功");
                  this.getDictionaryTypes();
                }else{
                  this.$message.error("系统繁忙，请稍后重试【500】")
                }
                this.saveLoading = false;
                this.saveFormVisible = false;
              }).catch(res => {
                this.$message.error("系统繁忙，请稍后重试【400,404】")
              })
            });
          }
        });
      },


      // detail表
      //1.分页查询：获取字典明细列表
      getDictionarydetails() {
        let para = {
          currentPage1: this.currentPage1,
          pageSize1: this.pageSize1,

          // 高级查询的参数
          keyword: this.keyword
        };
        // 显示模态框
        this.listLoading1 = true;
        this.$http.post("/detail", para).then((res) => {
          //给totals赋值 - 计算分页栏
          this.totals1 = res.data.totals;
          //赋值分页数据 - 显示当前页的分页数据
          this.dictionarydetails = res.data.data;
          //隐藏加载框/忙等框
          this.listLoading1 = false;
          //NProgress.done();
        }).catch(res => {
          // 失败后得回调函数，弹出错误信息提示框
          this.$message.error('系统繁忙，请稍后重试!!!');
        })
      },
      // 2.点击事件：点击某一页触发方法
      handleCurrentChange1(val) {
        // 获取当前页
        this.currentPage1 = val;
        this.getDictionarydetails();
      },

      // 3.高级查询
      keywordQuery(){
        // 让每次高级查询都从第一页去查询，
        // 不设置的话就会从当前页去查询，数据没有那么多就会查询不出来
        this.currentPage1 = 1;
        this.getDictionarydetails();
      },


      //4.删除功能
      handleDel1: function (index, row) {
        // 成功后执行的代码
        this.$confirm('确认删除该记录吗?', '温馨提示', {
          type: 'warning'
        }).then(() => {
          // 显示加载框
          this.listLoading1 = true;
          // 获取对象中的id
          let id = row.id ;
          // 发送异步请求
          this.$http.delete("/detail/"+id).then((res) => {
            if (res.data.success){
              // 成功后打印删除成功信息
              this.$message.success("删除成功")

              // 获取当前总页数
              var totalPage =  Math.ceil(this.totals1/this.pageSize1);
              // 如果总之那个页数等于当前页并且当前页大于1并且事最后一条数据
              if (totalPage == this.currentPage1 && this.currentPage1>1 && (this.totals1 - 1) %this.pageSize1 == 0){
                // 返回前一页
                this.currentPage1 = this.currentPage1 - 1;
              }

              this.getDictionarydetails()
            }else{
              this.$message.error("系统繁忙，稍后在试！！！【500】")
            }
            // 不管删除是否成功都要关闭加载框
            this.listLoading1 = false;
          }).catch(res => {
            this.$message.error("系统繁忙，稍后在试！！！【404,400】")
          })
        }).catch(() => {
        });
      },
      // 5.选项改变的时候触发方法
      selsChange1: function (sels1) {
        // 获取选中的对象
        this.sels1 = sels1;
      },

      //6.批量删除
      batchRemove1: function () {
        var ids = this.sels1.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '温馨提示', {
          type: 'warning'
        }).then(() => {
          // 显示加载框
          this.listLoading1 = true;
          // 发送异步请求
          this.$http.patch("/detail", ids).then(res => {
            // 判断是否删除成功
            if (res.data.success) {
              // 删除成功给个提示
              this.$message.success("删除成功");
              // 批量删除成功返回第一页
              this.currentPage1 = 1;
              this.getDictionarydetails();
            } else {
              this.$message.error("系统繁忙，请稍后重试！！！【500】");
            }
            // 关闭加载框
            this.listLoading1 = false;
          }).catch(res => {
            this.$message.error("系统繁忙，请稍后重试！！！【400,404】");
          })
        }).catch(() => {
        });
      },

      // 7.点击添加修改弹出修改对话框
      handleEdit1: function (index, row) {
        // 登录信息对象
        this.getTypes();
        this.title1 = " 修 改 ";
        this.saveFormVisible1 = true;
        this.saveForm1 = Object.assign({}, row);
      },
      //8.点击添加弹出修改对话框
      handleAdd1: function () {
        this.saveFormVisible1 = true;
        this.saveForm1 = {
          id: null,
          name: '',
          types_id: null,
          types: null
        };
        // 登录信息对象
        this.getTypes();
        this.title1 = " 添 加 ";
        //显示对话框
        this.saveFormVisible1 = true;
      },
      //9.点击提交按钮添加和修改
      saveSubmit1: function () {
        this.$refs.saveForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '温馨提示', {}).then(() => {
              this.saveLoading1 = true;
              let para = Object.assign({}, this.saveForm1);
              // 发送异步请求
              this.$http.put("/detail",para).then(res => {
                if (res.data.success){
                  this.$message.success('操作成功');
                  this.getDictionarydetails()
                }else{
                  this.$message.error('系统繁忙，请稍后重试!!!【500】');
                }
                // 关闭对话框
                this.saveFormVisible1 = false;
                // 关闭加载框
                this.saveLoading1 = false;
              }).catch(res => {
                this.$message.error('系统繁忙，请稍后重试!!!【400,404】');
              })
            });
          }
        });
      },

      getTypes(){
        this.$http.get("/type").then(res => {
          // 将获取到的数据赋值给types
          this.types = res.data;
        })
      }

    },


		mounted() {
			this.getDictionaryTypes();
      this.getDictionarydetails();
		}
	}

</script>

<style scoped>

</style>